# == Schema Information
#
# Table name: hydropowers
#
#  id               :bigint           not null, primary key
#  electric_balance :decimal(, )
#  total_day        :integer
#  water_balance    :decimal(, )
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#
class Hydropower < ApplicationRecord
	has_many :dorms, :dependent => :destroy
	has_many :dorm_users, :dependent => :destroy

	ransacker :excel_tag do
  end

	def import_excel(file)
		xlsx = file =~ /\.xlsx$/ ? Roo::Excelx.new(file) : Roo::Excel.new(file)
    sheet = xlsx.sheet('寝室')
    sum = sheet.parse(header_search: []).count
    # 遍历每一行
    sheet.parse(header_search: []).each_with_index do |hash, i|
      # 初始化 数据
      user_rank_hash = {name: nil, total_amount: nil}
      # 遍历一行的每一列
      hash.each do |k,v|
        heard_key = k.to_s
        next unless heard_key =~ /寝室名称/ || heard_key =~ /寝室电费金额/
        user_rank_hash[:name]         = v.to_s.chomp("\t") if heard_key =~ /寝室名称/
        user_rank_hash[:total_amount] = v.to_s.chomp("\t") if heard_key =~ /寝室电费金额/
      end
      p '---1-----'
      p user_rank_hash[:name]
      p user_rank_hash[:total_amount]
      raise '数据缺失，请检查寝室名称和寝室电费金额确保不为空' if user_rank_hash[:name].blank? || user_rank_hash[:total_amount].blank?
			self.dorms.create(name: user_rank_hash[:name], electric_balance: user_rank_hash[:total_amount])
    end

		sheet2 = xlsx.sheet('员工')
		sheet2.parse(header_search: []).each_with_index do |hash, i|
      # 初始化 数据
      user_rank_hash = {name: nil, day_count: nil, pay_balance: nil, dark_name: nil}
      # 遍历一行的每一列
      hash.each do |k,v|
        heard_key = k.to_s
        next unless heard_key =~ /员工姓名/ || heard_key =~ /住宿天数/ || heard_key =~ /已缴费/ || heard_key =~ /所属寝室/
        user_rank_hash[:name]         = v.to_s.chomp("\t") if heard_key =~ /员工姓名/
        user_rank_hash[:day_count] = v.to_s.chomp("\t") if heard_key =~ /住宿天数/
        user_rank_hash[:pay_balance] = v.to_s.chomp("\t") if heard_key =~ /已缴费/
        user_rank_hash[:dark_name] = v.to_s.chomp("\t") if heard_key =~ /所属寝室/
      end
      raise '数据缺失，请检查员工姓名、住宿天数、所属寝室 确保不为空' if user_rank_hash[:name].blank? || user_rank_hash[:day_count].blank? || user_rank_hash[:dark_name].blank?
			dorm = self.dorms.find_by(name: user_rank_hash[:dark_name])
			raise "当前寝室不存在--> #{user_rank_hash[:dark_name]},请先去导入" if dorm.blank?
			dorm.dorm_users.create(hydropower_id: self.id, day_count: user_rank_hash[:day_count], name: user_rank_hash[:name], pay_balance: user_rank_hash[:pay_balance])

    end
	end

	def export_excel
    header = ['所属寝室', '所属寝室总缴电费', '员工名称', '住宿天数', '应缴水费', '应缴电费', '已缴纳', '实际应缴水费', '实际应缴电费', '实际缴费合计']
    output = Redmine::ExcelUtil.generate do |wb|
      styles = Redmine::ExcelUtil.get_sheet_style(wb)
      wb.add_worksheet(name: "List") do |sheet|
        sheet.add_row header, style: styles[:header]
        #导入内容
				self.dorm_users.each do |dorm_user|
					day_count_water = (self.water_balance / self.dorm_users.sum(:day_count)) * dorm_user.day_count
					# pay_dorm_user_count = dorm_user.dorm.dorm_users.count
					water_pay = (self.water_balance / self.dorm_users.sum(:day_count)) * dorm_user.dorm.dorm_users.where('pay_balance > ?', 0).sum(:day_count)

					avatag_total = dorm_user.dorm.avatag_total
					electric_balance = (dorm_user.dorm.electric_balance - (dorm_user.dorm.dorm_users.sum(:pay_balance) - water_pay))
					day_count_electric = (dorm_user.dorm.electric_balance / dorm_user.dorm.dorm_users.sum(:day_count)) * dorm_user.day_count
					day_count_electric2 = (electric_balance / dorm_user.dorm.dorm_users.where.not('pay_balance > ?', 0).sum(:day_count)) * dorm_user.day_count
					final_total_amount = dorm_user.pay_balance > 0 ? 0 : (day_count_water + day_count_electric2).round(2)

					row = [dorm_user.dorm&.name, dorm_user.dorm&.electric_balance.round(2), dorm_user.name, dorm_user.day_count, day_count_water.round(2), day_count_electric.round(2), dorm_user.pay_balance,
					dorm_user.pay_balance > 0 ? 0 : day_count_water.round(2),
					dorm_user.pay_balance > 0 ? 0 : day_count_electric2.round(2),
					final_total_amount
					]
					sheet.add_row row, types: :string, style: styles[:content]
				end
      end
    end
    output
  end
end
